package com.vshop.service.aspecial.impl;

import com.alibaba.fastjson.JSON;
import com.vshop.config.dto.ResultDataDto;
import com.vshop.config.dto.StringOutputConstant;
import com.vshop.entity.agatewaytobeijing.querydevice.beijing.response.QueryDeviceData;
import com.vshop.entity.agatewaytobeijing.querydevice.beijing.response.ResponseQueryDevice;
import com.vshop.entity.agatewaytobeijing.querydeviceorder.response.DevicePackageOrder;
import com.vshop.entity.agatewaytobeijing.querydeviceorder.response.ResponseQueryDeviceOrder;
import com.vshop.entity.agatewaytobeijing.querypartnercode.response.ResponseQueryPartnerByDeviceId;
import com.vshop.entity.alast.common.PartnerConfigContent;
import com.vshop.entity.alast.common.PartnerPackage;
import com.vshop.entity.alast.common.PartnerServicePlan;
import com.vshop.entity.alast.common.ServicePlanOrderRecord;
import com.vshop.entity.alast.single.order.request.RequestSingleOrder;
import com.vshop.entity.aspecial.activate.request.RequestFirstOrder;
import com.vshop.entity.aspecial.common.PartnerSpecialAccessRecord;
import com.vshop.entity.aspecial.common.PartnerSpecialDeviceLifecycle;
import com.vshop.entity.purchase.request.RequestPurchaseService;
import com.vshop.entity.purchase.response.ResponsePurchaseService;
import com.vshop.entity.requestinterface.Constant;
import com.vshop.mapper.alast.LogicProcessesMapper;
import com.vshop.mapper.aspecial.SpecialLogicMapper;
import com.vshop.service.PartnerInfoService;
import com.vshop.service.agatewaytobeijing.GatewayToBeijingService;
import com.vshop.service.impl.PurchaseLogicService;
import com.vshop.utils.HttpUtil;
import com.vshop.utils.Md5Encrypt;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 特殊接口请求辅助处理类
 *
 * @author lm
 * @date 2020/8/26 15:25
 */

@Component
@Slf4j
public class SpecialAssistService {
    @Autowired
    PurchaseLogicService purchaseService;
    @Autowired
    PartnerInfoService partnerService;
    @Resource
    private LogicProcessesMapper logicMapper;
    @Resource
    private SpecialLogicMapper specialLogicMapper;
    /**
     * 请求网关的服务类
     */
    @Autowired
    GatewayToBeijingService toBeijingService;

    /**
     * 直接请求网关xml接口的地址
     */
    @Value("${activate.orderUrl}")
    private String orderUrl;

    @Value("${activate.purchaseUrl}")
    private String purchaseUrl;


    /**
     * 验证卡片
     *
     * @param partnerCode
     * @param deviceCode
     * @param lang
     * @return
     */
    public String deviceVerify(ResponseQueryPartnerByDeviceId partner, String partnerCode, String deviceCode, String lang) {
        boolean b0 = partner != null && StringUtils.isNotBlank(partner.getCode()) && StringUtils.isNotBlank(partner.getMessage());

        if (b0) {
            //卡片不是领科的卡片，返回“没有设备”或者“设备信息不存在”
            boolean b1 = null == partner.getData() || "9999".equals(partner.getCode()) || "0009".equals(partner.getCode()) || "0010".equals(partner.getCode());
            if (b1) {
                log.info("【iccId验证】卡片不是领科的卡片，返回“没有设备”或者“设备信息不存在”");
                return StringOutputConstant.CODE_ONE;
            }
            //中文版验证过程
            if (StringOutputConstant.CODE_LANG_CN.equals(lang)) {

                //如果iccId能够正常查询到partnerCode，但是返回的partnerCode却不是请求的partnerCode，
                if (StringUtils.isNotBlank(partner.getData().getPartner_code()) && !partnerCode.equals(partner.getData().getPartner_code())) {
                    log.info("【iccId验证】系领之飞或者领科其他家卡片，提示“无效卡片”");
                    return "无效卡片";
                }

                //如果以上步骤都没有问题证明至少是领科出的卡没有错，我们继续用获得的企业编码去做更多的查询
                String secret = partnerService.getSecretByPartnerCode(partner.getData().getPartner_code());

                if (StringUtils.isBlank(secret)) {
                    log.info("【iccId验证】" + partner.getData().getPartner_code() + ";当前企业secret有误请联系管理员！");
                    return "当前企业密钥可能有误，请联系管理员";
                }

                //如果以上都还是没有问题，现在查询下设备的状态，为了屏蔽掉在内部库存期或废弃回收期内的卡片，不让这类卡片订购套餐。
                ResponseQueryDevice device = toBeijingService.queryDevice(deviceCode.trim(), partner.getData().getPartner_code(), secret);

                if (null != device) {

                    boolean b2 = StringUtils.isNotBlank(device.getCode()) && StringUtils.isNotBlank(device.getMessage()) && null != device.getData() && Constant.RET_CODE_0000.equals(device.getCode());

                    if (!b2) {
                        log.info("【iccId验证】查询设备信息返回有误：" + device.getMessage());
                        return device.getMessage();
                    }

                    //如果查询结果返回还是没有问题，那么我们现在来屏蔽掉在内部库存期或废弃回收期内的卡片，不让这类卡片订购套餐。
                    QueryDeviceData tr = device.getData();
                    //如果卡片在内部库存期或废弃回收期内内，表明卡片过期了，不允许Top-up
                    boolean b3 = StringUtils.isNotBlank(tr.getLifecycle()) && (tr.getLifecycle().equals(StringOutputConstant.CODE_FOUR) || tr.getLifecycle().equals(StringOutputConstant.CODE_FIVE));
                    if (b3) {
                        log.info("【iccId验证】SIM卡已过期");
                        return "SIM卡已过期";
                    }
                    //走这边标识卡片没有过期，不管查询到的渠道编码是不是跟请求上来的一致，我们都用新的渠道编码去做覆盖然后继续去查询当前企业是否具有正在使用的某种方案
                    List<PartnerConfigContent> configList = logicMapper.queryPlan(partner.getData().getPartner_code());

                    if (null == configList || configList.isEmpty()) {
                        log.info("【iccId验证】归属企业无正在使用方案");
                        return "没有方案，请联系:Service@linksfield.net";
                    }

                    //领科的卡片验证成功
                    return StringOutputConstant.CODE_ZERO;

                } else {
                    log.info("【iccId验证】中文查询设备信息返回有误");
                    return "请输入有效的卡片iccId";
                }

            } else {
                //除了中文以外的其他语言版
                //如果iccId能够正常查询到partnerCode，但是返回的partnerCode却不是请求的partnerCode，
                if (StringUtils.isNotBlank(partner.getData().getPartner_code()) && !partnerCode.equals(partner.getData().getPartner_code())) {
                    log.info("【iccId验证】系领之飞或者领科其他家卡片，提示“无效卡片”");
                    return "Invalid Card";
                }

                //如果以上步骤都没有问题证明至少是领科出的卡没有错，我们继续用获得的企业编码去做更多的查询
                String secret = partnerService.getSecretByPartnerCode(partner.getData().getPartner_code());
                if (StringUtils.isBlank(secret)) {
                    log.info("【iccId验证】英文" + partner.getData().getPartner_code() + ";当前企业secret有误请联系管理员！");
                    return "Secret is wrong, Contact the administrator";
                }

                //如果以上都还是没有问题，现在查询下设备的状态，为了屏蔽掉在内部库存期或废弃回收期内的卡片，不让这类卡片订购套餐。
                ResponseQueryDevice device = toBeijingService.queryDevice(deviceCode.trim(), partner.getData().getPartner_code(), secret);

                if (null != device) {

                    boolean b2 = StringUtils.isNotBlank(device.getCode()) && null != device.getData() && Constant.RET_CODE_0000.equals(device.getCode());
                    if (!b2) {
                        log.info("【iccId验证】英文查询设备信息返回有误：" + device.getMessage());
                        return "Please enter a valid iccId";
                    }

                    //如果查询结果返回还是没有问题，那么我们现在来屏蔽掉在内部库存期或废弃回收期内的卡片，不让这类卡片订购套餐。
                    QueryDeviceData tr = device.getData();

                    //如果卡片在内部库存期或废弃回收期内内，表明卡片过期了，不允许Top-up
                    boolean b3 = StringUtils.isNotBlank(tr.getLifecycle()) && (tr.getLifecycle().equals(StringOutputConstant.CODE_FOUR) || tr.getLifecycle().equals(StringOutputConstant.CODE_FIVE));
                    if (b3) {
                        log.info("【iccId验证】英文SIM卡已过期");
                        return "The sim card has expired";
                    }

                    //走这边标识卡片没有过期，不管查询到的渠道编码是不是跟请求上来的一致，我们都用新的渠道编码去做覆盖然后继续去查询当前企业是否具有正在使用的某种方案
                    List<PartnerConfigContent> configList = logicMapper.queryPlan(partner.getData().getPartner_code());

                    if (null == configList || configList.isEmpty()) {
                        log.info("【iccId验证】英文归属企业无正在使用方案");
                        return "No Plan,Please Contact:Service@linksfield.net";
                    }

                    //领科的卡片验证成功
                    return StringOutputConstant.CODE_ZERO;

                } else {
                    log.info("【iccId验证】英文查询设备信息程序有误");
                    return "The response exception";
                }
            }
        } else {
            log.info("【iccId验证】查询渠道编码程序有误");
            return "The response exception";
        }

    }

    /**
     * 先将特殊接口访问打开链接的访问记录入库
     *
     * @param tradeType
     * @param tradeTime
     * @param mark
     * @param noticeUrl
     * @param actionUrl
     * @param requestOrderId
     * @param deviceCode
     * @param partnerCode
     */
    public void insertSpecialAccessRecord(String tradeType, String tradeTime, String mark, String noticeUrl, String actionUrl, String requestOrderId, String deviceCode, String partnerCode, String area) {

        //先将特殊接口访问打开链接的访问记录入库
        Integer count = specialLogicMapper.querySpecialAccessRecord(partnerCode, deviceCode, requestOrderId);
        if (count <= 0) {
            PartnerSpecialAccessRecord record = new PartnerSpecialAccessRecord();
            record.setPartnerCode(partnerCode);
            record.setDeviceCode(deviceCode);
            record.setRequestOrderId(requestOrderId);
            record.setNoticeUrl(noticeUrl);
            record.setActionUrl(actionUrl);
            record.setMark(mark);
            record.setArea(area);
            record.setTradeTime(tradeTime);
            record.setTradeType(tradeType);
            specialLogicMapper.insertSpecialAccessRecord(record);
        }

    }


    /**
     * 在激活操作中先去下单
     * <p>
     * 就是造假单
     *
     * @param planList
     * @param request
     * @return
     */
    public ResultDataDto activateOrder(List<PartnerServicePlan> planList, RequestFirstOrder request, String orderNo, String payId, String payAmount, String payCurrency, String orderPeriod) {

        RequestSingleOrder singleOrder = null;

        for (PartnerServicePlan se : planList) {
            singleOrder = new RequestSingleOrder();
            singleOrder.setTradeType("singleOrder");
            singleOrder.setTradeTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            singleOrder.setVersion(request.getVersion());
            singleOrder.setTerminal("0");
            singleOrder.setMark(request.getMark());
            singleOrder.setLanguageCode(se.getLanguageType());
            singleOrder.setPartnerCode(request.getPartnerCode());
            singleOrder.setDeviceCode(request.getDeviceCode());
            singleOrder.setOrderNo(orderNo);
            singleOrder.setPayId(payId);
            singleOrder.setPayAmount(payAmount);
            singleOrder.setPayCurrency(payCurrency);
            singleOrder.setPackageCode(se.getPackageCode());
            singleOrder.setServicePlanCode(se.getServicePlanCode());
            singleOrder.setServicePlanShowName(se.getServicePlanShowName());
            singleOrder.setOrderPeriod(orderPeriod);
            singleOrder.setRequestOrderId(request.getRequestOrderId());
            singleOrder.setAutoRenewal("0");
            singleOrder.setSign(Md5Encrypt.jsonSign(singleOrder));
        }

        // 开始组装json
        String requestStr = JSON.toJSONString(singleOrder);
        log.info("领科卡首次激活【下单】请求：" + requestStr);
        // 发送post请求，并接收返回结果
        String returnStr = HttpUtil.httpPost(orderUrl, requestStr);
        log.info("url=" + orderUrl);

        log.info("领科卡首次激活【下单】响应：" + returnStr);
        return JSON.parseObject(returnStr, ResultDataDto.class);

    }

    /**
     * 模拟支付成功处理数据的方法
     * <p>
     * payType=0第三方自行支付
     *
     * @param orderNo
     * @param payId
     * @param payAmount
     * @param transactionId
     * @param partnerCode
     */
    public Integer orderDataProcessing(String partnerCode, String orderNo, String payId, String payAmount, String transactionId) {

        ServicePlanOrderRecord record = new ServicePlanOrderRecord();
        record.setPartnerCode(partnerCode);
        record.setOrderNo(orderNo);
        record.setPayId(payId);
        record.setPayRst("SUCCESS");
        record.setPayType("0");
        record.setPayAccount("1");
        record.setTransactionId(transactionId);
        record.setPayUnit(payAmount);
        record.setSettlePrice(payAmount);
        record.setPayStatus("1");
        record.setPayResultDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return specialLogicMapper.insertSpecialPayData(record);
    }

    /**
     * 去调用内部订购套餐的接口
     *
     * @param payId
     * @return
     */
    public String orderServicePlan(String payId) {

        //订购需要分不同的情况用不同的参数当做单号
        List<ServicePlanOrderRecord> orderList = specialLogicMapper.queryOrderRecordList(payId);

        if (null != orderList && !orderList.isEmpty()) {

            for (ServicePlanOrderRecord record : orderList) {

                String secret = partnerService.findSecretByPartnerCode(record.getPartnerCode());

                if (StringUtils.isNotBlank(record.getPartnerCode())) {

                    if (StringUtils.isNotBlank(secret)) {

                        if (StringUtils.isNotBlank(record.getDeviceCode()) && StringUtils.isNotBlank(record.getPackageName()) && StringUtils.isNotBlank(record.getPackageCode()) &&
                                StringUtils.isNotBlank(record.getPackageType()) && StringUtils.isNotBlank(record.getOrderPeriod()) && StringUtils.isNotBlank(record.getPayUnit()) &&
                                StringOutputConstant.CODE_SUCCESS.equalsIgnoreCase(record.getPayRst()) && StringUtils.isNotBlank(record.getPayId()) && StringUtils.isNotBlank(record.getOrderNo())) {

                            List<PartnerPackage> packageList = specialLogicMapper.queryType(record.getPartnerCode(), record.getPackageCode());

                            if (null != packageList && !packageList.isEmpty()) {

                                for (PartnerPackage p : packageList) {
                                    log.info("单号：" + record.getPayId() + "卡片：" + record.getDeviceCode() + "符合订购套餐规则");
                                    /*符合要求就去请求订购套餐*/
                                    ResponsePurchaseService re = purchaseServicePlan(secret, p.getBuyType(), p.getPriceType(), record);

                                    if (null != re) {
                                        if (StringUtils.isNotBlank(re.getRetCode())) {
                                            if (Constant.RET_CODE_0000.equals(re.getRetCode())) {
                                                log.info("【订购套餐】成功:" + re.getResultInfo());
                                                return StringOutputConstant.CODE_SUCCESS;
                                            } else {
                                                log.info("【订购套餐】失败：" + re.getResultInfo());
                                                return StringOutputConstant.CODE_FAIL;
                                            }
                                        } else {
                                            log.info("【订购套餐】返回似乎有问题");
                                        }
                                    } else {
                                        log.info("【订购套餐】返回似乎有问题");
                                    }
                                }
                            } else {
                                log.info("为查询到对应的套餐");
                            }
                        } else {
                            log.info("查询到的订单有数据不符合订购");
                        }
                    } else {
                        log.info("当前企业可能没有secret，需要联系管理员");
                    }
                } else {
                    log.info("查询到了订单但partnerCode为空，进入异常处理");
                }
            }
        } else {
            log.info("未查询到：" + payId + "成功的支付订单记录");
        }
        return "";
    }

    /**
     * 调用内部去北京订购套餐的接口 /purchaseServicePlan
     *
     * @param secret
     * @param buyType
     * @param priceType
     * @param record
     * @return
     */
    private ResponsePurchaseService purchaseServicePlan(String secret, String buyType, String priceType, ServicePlanOrderRecord record) {
        /*支付结果通知回到数据库从支付平台会跳转到订购套餐服务项目去执行业务*/
        RequestPurchaseService pur = new RequestPurchaseService();
        pur.setPartnerCode(record.getPartnerCode());
        pur.setSecret(secret);
        pur.setDeviceCode(record.getDeviceCode());
        pur.setPackageName(record.getPackageName());
        pur.setPackageCode(record.getPackageCode());
        pur.setPackageType(record.getPackageType());
        pur.setOrderPeriod(record.getOrderPeriod());
        pur.setAmount(record.getPayUnit());
        pur.setBuyType(buyType);
        pur.setPriceType(priceType);
        pur.setPayId(record.getPayId());
        pur.setOrderNo(record.getOrderNo());
        pur.setPayRst(record.getPayRst());
        pur.setPayType(record.getPayType());
        pur.setPayMode(record.getPayMode());

        // 开始组装json
        String requestStr = JSON.toJSONString(pur);
        log.info("领科卡激活【订购套餐】请求：" + requestStr);
        // 发送post请求，并接收返回结果
        String returnStr = HttpUtil.httpPost(purchaseUrl, requestStr);
        log.info("url=" + purchaseUrl);

        log.info("领科卡激活【订购套餐】响应：" + returnStr);
        return JSON.parseObject(returnStr, ResponsePurchaseService.class);
    }

    /**
     * 根据套餐类型设置订单周期的单位
     *
     * @param packageType
     * @return
     */
    public String setUpOrderPeriodUnit(String packageType) {
        if (StringUtils.isNotBlank(packageType)) {
            if (StringOutputConstant.CODE_ZERO.equals(packageType) || StringOutputConstant.CODE_ONE.equals(packageType)) {
                return "天";
            } else if (StringOutputConstant.CODE_TWO.equals(packageType)) {
                return "个月";
            } else if (StringOutputConstant.CODE_THREE.equals(packageType)) {
                return "个季度";
            } else if (StringOutputConstant.CODE_FOUR.equals(packageType)) {
                return "个半年";
            } else if (StringOutputConstant.CODE_FIVE.equals(packageType)) {
                return "年";
            } else {
                return "个半年";
            }
        }
        return "个半年";
    }

    /**
     * 根据payType的不同数值得出具体的支付方式的文字
     *
     * @param payType
     * @return
     */
    public String setUpOrderPayType(String payType) {
        if (StringUtils.isNotBlank(payType)) {
            if (StringOutputConstant.CODE_ZERO.equals(payType)) {
                return "第三方自行支付";
            } else if (StringOutputConstant.CODE_ONE.equals(payType)) {
                return "微信支付";
            } else if (StringOutputConstant.CODE_TWO.equals(payType)) {
                return "payPal支付";
            } else if (StringOutputConstant.CODE_THREE.equals(payType)) {
                return "Stripe信用卡支付";
            }
        }
        return "第三方自行支付";
    }

    /**
     * 3.0上面订单连接接口：计算归属领科的卡片理论上的剩余可用天数
     *
     * @param orders
     * @return
     */
    public String retrievalRemainingDaysNew(ResponseQueryDeviceOrder orders) {
        //初始化一个剩余天数
        String remainingDays = "0";
        try {
            //1,获取到所有订单
            List<DevicePackageOrder> packageOrderList = orders.getData().getPackageOrder();
            //2,算下有几条未启用的订单：
            int countUnused = 0;
            for (int i = 0; i < packageOrderList.size(); i++) {
                DevicePackageOrder packageOrder = packageOrderList.get(i);
                //计算list中有几个未启用的订单
                if (packageOrder != null && packageOrder.getOrderStatus().equals(StringOutputConstant.CODE_ONE)) {
                    countUnused = countUnused + 1;
                }
            }
            //未启用的订单剩余天数
            int daysUnused = 0;
            //正在使用的订单还剩余的天数
            int daysUsing = 0;
            //通过循环订单来计算可用剩余天数
            for (DevicePackageOrder order : packageOrderList) {
                /*3，开始使用订单计算剩余天数*/
                if (order.getOrderStatus().equals(StringOutputConstant.CODE_TWO)) {
                    SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyyMMdd");
                    /*预计结束时间*/
                    Date expireDate = simpleFormat.parse(order.getExpireDate());
                    /*今天的日期*/
                    Date now = simpleFormat.parse(simpleFormat.format(new Date()));
                    long expire = expireDate.getTime();
                    long to = now.getTime();
                    daysUsing = (int) ((expire - to) / (1000 * 60 * 60 * 24));
                }
                /*4，未启用订单计算剩余天数*/
                else if (order.getOrderStatus().equals(StringOutputConstant.CODE_ONE)) {
                    if (StringOutputConstant.CODE_ZERO.equals(order.getPackageType()) || StringOutputConstant.CODE_ONE.equals(order.getPackageType())) {
                        //符合未启用且套餐属于日程套餐或者日流量套餐的订单周期
                        String num = Integer.parseInt(order.getOrderPeriod()) + "";
                        daysUnused = Integer.parseInt(num) * countUnused;
                    } else if (StringOutputConstant.CODE_TWO.equals(order.getPackageType())) {
                        //符合未启用且套餐属于月流量套餐的订单周期
                        String num = Integer.parseInt(order.getOrderPeriod()) + "";
                        daysUnused = Integer.parseInt(num) * countUnused * 30;
                    } else if (StringOutputConstant.CODE_THREE.equals(order.getPackageType())) {
                        //符合未启用且套餐属于季度流量套餐的订单周期
                        String num = Integer.parseInt(order.getOrderPeriod()) + "";
                        daysUnused = Integer.parseInt(num) * countUnused * 30 * 3;
                    } else if (StringOutputConstant.CODE_FOUR.equals(order.getPackageType())) {
                        //符合未启用且套餐属于半年流量套餐的订单周期
                        String num = Integer.parseInt(order.getOrderPeriod()) + "";
                        daysUnused = Integer.parseInt(num) * countUnused * 30 * 6;
                    } else if (StringOutputConstant.CODE_FIVE.equals(order.getPackageType())) {
                        //符合未启用且套餐属于年流量套餐的订单周期
                        String num = Integer.parseInt(order.getOrderPeriod()) + "";
                        daysUnused = Integer.parseInt(num) * countUnused * 30 * 12;
                    }
                }
            }
            log.info("领科卡已使用的订单剩余天数：" + daysUsing + ";领科卡未启用的订单剩余天数：" + daysUnused);
            remainingDays = String.valueOf(daysUnused + daysUsing);
            return remainingDays;
        } catch (Exception e) {
            log.info("领科卡计算剩余天数过程异常：" + e);
        }
        return remainingDays;
    }

    /**
     * 计算不归属领科的卡片理论上的剩余可用天数
     *
     * @param partnerCode
     * @param deviceCode
     * @return
     */
    public String retrievalNonRemainingDays(String partnerCode, String deviceCode) {
        //初始化一个剩余天数
        String remainingDays = "0";
        try {
            //1,获取到所有订单
            List<PartnerSpecialDeviceLifecycle> packageOrderList = specialLogicMapper.querySpecialDeviceOrder(partnerCode, deviceCode);
            if (null != packageOrderList && !packageOrderList.isEmpty()) {
                //2,算下有几条未启用的订单：
                int countUnused = 0;
                for (int i = 0; i < packageOrderList.size(); i++) {
                    PartnerSpecialDeviceLifecycle packageOrder = packageOrderList.get(i);
                    //计算list中有几个未启用的订单
                    if (packageOrder != null && packageOrder.getStatus().equals(StringOutputConstant.CODE_ONE)) {
                        countUnused = countUnused + 1;
                    }
                }
                //未启用的订单剩余天数
                int daysUnused = 0;
                //正在使用的订单还剩余的天数
                int daysUsing = 0;
                //通过循环订单来计算可用剩余天数
                for (PartnerSpecialDeviceLifecycle order : packageOrderList) {
                    //3，开始使用订单计算剩余天数
                    if (order.getStatus().equals(StringOutputConstant.CODE_TWO)) {
                        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        //预计结束时间
                        Date expireDate = simpleFormat.parse(order.getOrderExpireDate());
                        //今天的日期
                        Date now = simpleFormat.parse(simpleFormat.format(new Date()));
                        long expire = expireDate.getTime();
                        long to = now.getTime();
                        daysUsing = (int) ((expire - to) / (1000 * 60 * 60 * 24));
                    }
                    //4，未启用订单计算剩余天数
                    else if (order.getStatus().equals(StringOutputConstant.CODE_ONE)) {

                        if (StringOutputConstant.CODE_ONE.equals(order.getPackageType()) || StringOutputConstant.CODE_ZERO.equals(order.getPackageType())) {
                            //符合未启用且套餐属于日程套餐或者日流量套餐的订单周期
                            String num = Integer.parseInt(order.getOrderPeriod()) + "";
                            daysUnused = Integer.parseInt(num) * countUnused;
                        } else if (StringOutputConstant.CODE_TWO.equals(order.getPackageType())) {
                            //符合未启用且套餐属于月流量套餐的订单周期
                            String num = Integer.parseInt(order.getOrderPeriod()) + "";
                            daysUnused = 30 * Integer.parseInt(num) * countUnused;
                        } else if (StringOutputConstant.CODE_THREE.equals(order.getPackageType())) {
                            //符合未启用且套餐属于季度流量套餐的订单周期
                            String num = Integer.parseInt(order.getOrderPeriod()) + "";
                            daysUnused = 30 * 3 * Integer.parseInt(num) * countUnused;
                        } else if (StringOutputConstant.CODE_FOUR.equals(order.getPackageType())) {
                            //符合未启用且套餐属于半年流量套餐的订单周期
                            String num = Integer.parseInt(order.getOrderPeriod()) + "";
                            daysUnused = 30 * 6 * Integer.parseInt(num) * countUnused;
                        } else if (StringOutputConstant.CODE_FIVE.equals(order.getPackageType())) {
                            //符合未启用且套餐属于年流量套餐的订单周期
                            String num = Integer.parseInt(order.getOrderPeriod()) + "";
                            daysUnused = 30 * 12 * Integer.parseInt(num) * countUnused;
                        }
                    }
                }
                log.info("非领科卡已使用的订单剩余天数：" + daysUsing + ";非领科卡未启用的订单剩余天数：" + daysUnused);
                remainingDays = String.valueOf(daysUnused + daysUsing);
                return remainingDays;
            }
        } catch (Exception e) {
            log.info("非领科卡计算剩余天数过程异常：" + e);
        }
        return remainingDays;
    }

}
